<template>
  <div class="user-box" v-loading="loading">
    <div>
      <topTitle title="用户信息" style="padding-left: 30px" />
      <!-- 上方用户信息 -->
      <div class="user-top">
        <div class="user-area">
          <div class="user_img">
            <img :src="userVo.userImage" alt="" />
          </div>
          <div class="user-name">
            <div>{{ userVo.nickName || "-" }}</div>
            <div>
              <span>{{ userVo.userId }}</span>
              <span style="margin-left: 5px;" class="table-btn" @click="copyUserId(userVo.userId)">复制</span>
            </div>
            <div>{{ userVo.roleNames }}</div>
          </div>
          <el-button
            type="success"
            size="small"
            class="add-sub"
            @click="roleFlag = true"
            >设置角色</el-button
          >
          <el-button type="primary" size="small" class="add-sub" @click="addSub"
            >添加备注</el-button
          >
        </div>
        <div class="actions">
          <div class="user_btn">
            <!-- <el-button
              type="danger"
              class="btn"
              style="margin-left: 20px"
              @click="addBlackName(1)"
              >添加黑名单</el-button
            > -->
            <div v-if="userVo.userStatus === '0'"><el-button style="width:100%; margin-right: 10px" type="info" @click="updateStatus('1')">设为正常用户</el-button></div>
            <AddBlackName :sourceType="1" @updateList="updateList" :userId="userVo.userId"/>
            <!-- <div v-if="userVo.userStatus !== '1'">
                            <div><el-button size="small" style="width:100%;" type="info" @click="updateStatus('1')">设为正常用户</el-button></div>
                        </div>
                        <div v-if="userVo.userStatus !== '-2'">
                            <div style="position: relative;">
                                <el-button size="small" style="width:100%;" type="primary" @click="updateStatus('-2')">冻结钱包</el-button>
                                <template>
                                    <el-tooltip style="position: absolute; top: -5px; right: -16px; cursor:pointer;" class="item" effect="dark" placement="top">
                                        <div slot="content">金币和趣宝石不可消费、不可充值，仍可登录app</div>
                                        <i class="el-icon-question"></i>
                                    </el-tooltip>
                                </template>
                            </div>
                        </div>
                        <div v-if="userVo.userStatus !== '-1'">
                            <div style="position: relative;">
                                <el-button size="small" style="width:100%;" type="danger" @click="updateStatus('-1')">封 禁</el-button>
                                <template>
                                    <el-tooltip style="position: absolute; top: -5px; right: -16px; cursor:pointer;" class="item" effect="dark" placement="top">
                                        <div slot="content">账号禁止使用app</div>
                                        <i class="el-icon-question"></i>
                                    </el-tooltip>
                                </template>
                            </div>
                        </div> -->
          </div>
        </div>
      </div>
      <!-- 处罚记录 -->
      <topTitle title="处罚记录" style="padding-left: 30px" />
      <div style="margin: 0 30px 30px">
        <BlackNameItem ref="balckItem" :userId="userId" @updateInfo="getUserView"/>
      </div>
      <!-- 备注列表 -->
      <div class="sub-wrap" v-if="userRemarks && userRemarks.length">
        <div class="sub-title">
          <span>备注</span>
        </div>
        <template>
          <div class="sub-list" :style="{ height: allFlag ? 'auto' : '200px' }">
            <div
              class="sub-row"
              v-for="item in userRemarks"
              :key="item.remarkId"
            >
              <div class="top">
                <div class="name">{{ item.nickName }}</div>
                <div class="time">{{ item.updateTime }}</div>
                <i
                  class="el-icon-edit"
                  style="color: #409eff; margin-right: 10px"
                  @click="addSub(item)"
                ></i>
                <i
                  class="el-icon-delete"
                  style="color: #f56c6c"
                  @click="delSub(item)"
                ></i>
              </div>
              <div class="bottom">{{ item.remark }}</div>
              <div style="color: #909399; font-size: 12px" v-if="item.source">
                来源：{{ item.source }}
                <span class="table-btn" @click="goDetail(item)">{{
                  item.number
                }}</span>
              </div>
            </div>
          </div>
          <div class="sub-botton" @click="showAll">
            <i class="el-icon-caret-bottom"></i>
          </div>
        </template>
      </div>
      <!-- 中部用户信息展示 -->
      <topTitle title="基本信息" style="padding-left: 30px" />
      <div class="user-msg">
        <div class="user-info-box">
          <div
            class="user-info-item"
            v-for="(arr, idx) in properties"
            :key="idx"
          >
            <div
              class="user_box"
              :style="{
                flex:
                  maxCount > arr.properties.length &&
                  index + 1 === arr.properties.length
                    ? maxCount - arr.properties.length + 1
                    : 1,
              }"
              v-for="(item, index) in arr.properties"
              :key="index"
            >
              <div class="user_title">{{ item.key }}</div>
              <div class="user_msg">
                <span
                  :style="{ color: item.value.includes('处罚') ? 'red' : '#393939' }"
                  >{{ item.value }}</span
                >
                <div
                  class="tip_waring"
                  v-if="item.properties && item.properties.length"
                >
                  <el-tooltip
                    style="margin-left: 10px"
                    effect="dark"
                    placement="top"
                  >
                    <div
                      slot="content"
                      style="margin-bottom: 4px"
                      v-for="(i, j) in item.properties"
                      :key="j"
                    >
                      {{ i.key }} &nbsp; {{ i.value }}
                    </div>
                    <i class="el-icon-question"></i>
                  </el-tooltip>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 底部表格信息 -->
      <topTitle title="账号信息" style="padding-left: 30px" />
      <div class="user-tabs">
        <el-tabs :value="userTabValue">
          <el-tab-pane label="绑定信息" name="first" :lazy="true">
            <bindInfoMsg :detail="userVo" />
          </el-tab-pane>
          <el-tab-pane
            :label="`托管账号「${
              (counts && counts.collocation_account) || 0
            }」`"
            :lazy="true"
          >
            <account :detail="userVo" />
          </el-tab-pane>
          <el-tab-pane
            :label="`租号业务「${(counts && counts.rent_account) || 0}」`"
            :lazy="true"
          >
            <rentWork
              :userId="userVo.userId"
              :rentAccount="counts.rent_account || 0"
            />
          </el-tab-pane>
          <el-tab-pane
            :label="`卖号业务「${(counts && counts.sell_account) || 0}」`"
            :lazy="true"
          >
            <sellWork :userId="userVo.userId" :nums="nums" :buyNum="counts && counts.sell_account"/>
          </el-tab-pane>
          <el-tab-pane label="启动器" :lazy="true">
            <!-- <gift /> -->
            <gameBox :detail="userVo" />
          </el-tab-pane>
          <el-tab-pane
            :label="`充值记录「${(counts && counts.recharge) || 0}」`"
            :lazy="true"
          >
            <payDetail :detail="userVo" />
          </el-tab-pane>
          <el-tab-pane
            :label="`提现记录「${(counts && counts.withdraw) || 0}」`"
            :lazy="true"
          >
            <moneyBox :detail="userVo" />
          </el-tab-pane>
          <el-tab-pane
            :label="`会话记录「${(counts && counts.chat) || 0}」`"
            :lazy="true"
          >
            <userTalk :detail="userVo" />
          </el-tab-pane>
          <el-tab-pane label="其它" :lazy="true">
            <otherWork :detail="userVo" />
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>
    <el-dialog :title="placeholderTxt" :visible.sync="banned" width="400px">
      <div style="padding: 10px 0 20px">
        <el-input v-model="remark" type="textarea"></el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="banned = false">取 消</el-button>
        <el-button type="primary" @click="getUpdateStatus">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 添加备注信息 -->
    <el-dialog :title="subTitle" :visible.sync="subFlag" width="400px">
      <div style="padding: 10px 0 20px">
        <el-input
          v-model="subContent"
          maxlength="200"
          show-word-limit
          type="textarea"
        ></el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="subFlag = false">取 消</el-button>
        <el-button type="primary" @click="subConfirm">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 设置角色 -->
    <el-dialog title="设置角色" :visible.sync="roleFlag" width="400px">
      <div class="role-wrap">
        <span style="margin-right: 10px">角色(多选)</span>
        <el-checkbox-group v-model="roleArr" @change="checkboxChange">
          <el-checkbox
            v-for="(item, idx) in checkList"
            :key="idx"
            :label="item.name"
          ></el-checkbox>
        </el-checkbox-group>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="roleFlag = false">取 消</el-button>
        <el-button type="primary" @click="setRole">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import bindInfoMsg from "../user/bindInfoMsg";
import rentWork from "../user/rentWork";
import sellWork from "../user/sellWork";
import gift from "@/views/task/gift/index.vue";
import otherWork from "../user/otherWork";
import videoBox from "../user/video";
import talkBox from "../user/talk";
import talkBackBox from "../user/talkBack";
import gameBox from "../user/game";
import moneyBox from "../user/money";
import account from "../user/account";
import payDetail from "../user/payDetail";
import userTalk from "../user/userTalk";
import tuoguanList from "@/views/quDeal/orderManage/tuoguanList.vue";
import rentList from "@/views/quDeal/orderManage/rentList.vue";
import { formatDate, conversion } from "@/utils/utils";
export default {
  components: {
    bindInfoMsg,
    rentWork,
    sellWork,
    otherWork,
    gift,
    videoBox,
    talkBox,
    talkBackBox,
    gameBox,
    moneyBox,
    account,
    payDetail,
    tuoguanList,
    rentList,
    userTalk
  },
  data() {
    return {
      banned: false,
      remark: "",
      status: 1,
      userId: "",
      userTabValue: "first",
      showUser: false,
      loading: false,
      detail: {},
      risk: {
        1: "账号近期存在因恶意被处罚历史，网络低活跃，被举报等因素",
        2: "疑似批量注册小号，近期存在严重违规或大量举报",
        3: "送检账号参数无法成功解析，请检查微信 OpenID 是否有误",
        4: "该账号在业务侧有过拉黑记录",
        5: "业务自行有添加过白名单记录",
        101: "存在 IP/设备/环境等因素的聚集性异常",
        102: "疑似自动机批量请求",
        104: "检查 wxtoken 参数，是否已经失效",
        201: "操作 IP/设备/环境存在异常。当前 IP 为非常用 IP 或恶意 IP 段",
        205: "传进来的 IP 地址为内网 IP 地址或者 IP 保留地址",
        206: "该设备存在异常的使用行为",
      },
      placeholderTxt: "",
      subFlag: false, // 添加备注信息
      subContent: "",
      subTitle: "添加备注",
      subRemarkId: null,
      properties: [],
      userRemarks: [],
      userVo: {},
      allFlag: false,
      maxCount: 0,
      counts: {},
      roleFlag: false,
      checkList: [
        {
          name: "平台运营",
          type: "limitDeal",
          status: 0,
        },
        {
          name: "交易顾问",
          type: "limitTrade",
          status: 0,
        },
      ],
      roleArr: [],
      nums: {}
    };
  },
  created() {
    if (this.$route.query.id) {
      this.userId = this.$route.query.id;
      this.getUserView();
      this.getAdviseUserTapCount()
    }
  },

  methods: {
    // 复制用户
    copyUserId(item) {
      const aux = document.createElement('textarea')
      aux.innerHTML = item
      document.body.appendChild(aux)
      aux.select()
      document.execCommand('copy')
      document.body.removeChild(aux)
      this.$message.success('复制成功')
    },
    // 获取数目
    async getAdviseUserTapCount() {
        let res = await this.$http.get(`/user/adviseUserTapCount?userId=${this.userId}`, { 
        })
        if (res.code === '0') {
            this.nums = res.data
        } else {
            this.$message({
                type: 'error',
                message: '系统异常，请稍后再试'
            })
        }
    },
    updateList () {
      this.$refs.balckItem.getList()
      this.loading = false;
      this.getUserView()
    },
    setRole() {
      let params = {
        adviseUser: this.roleArr.includes("交易顾问") ? 1 : 0,
        identity: this.roleArr.includes("平台运营") ? 1 : 0,
        userId: this.userId,
      };
      this.$http.post("/user/setRole", params).then((res) => {
        if (res.code === "0") {
          this.roleFlag = false;
          this.getUserView();
          this.$message({
            type: "success",
            message: "设置成功",
          });
        } else {
          this.$message({
            type: "error",
            message: res.desc,
          });
        }
      });
    },
    checkboxChange(arr) {
      this.roleArr = arr;
    },
    // 添加备注
    addSub(content) {
      this.subFlag = true;
      this.subContent = "";
      if (content && content.remarkId) {
        this.subRemarkId = content.remarkId;
        this.subTitle = "编辑备注";
        this.subContent = content.remark;
      }
    },
    goDetail(item) {
      if (!item.jumpType) {
        this.$message({
          type: "error",
          message: "跳转失败",
        });
      } else if (item.jumpType === 1) {
        const { href } = this.$router.resolve({
          path: "/seller/accTradingDetail",
          query: { dataId: item.businessId, status, type: 0 },
        });
        window.open(href, "_blank");
      } else if (item.jumpType === 2) {
        const { href } = this.$router.resolve({
          path: "/seller/tradOrderDetail",
          query: { orderId: item.businessId },
        });
        window.open(href, "_blank");
      }
    },
    // 展开全部
    showAll() {
      this.allFlag = !this.allFlag;
    },
    // 确定添加或编辑备注
    subConfirm() {
      let params = {
        beRemarkUserId: this.userId,
        remark: this.subContent,
      };
      let url = `/user/addRemark`;
      if (this.subRemarkId) {
        url = `/user/editRemark`;
        params.remarkId = this.subRemarkId || 0;
      }
      this.$http.post(url, params).then((res) => {
        if (res.code === "0") {
          this.subFlag = false;
          this.getUserView();
          this.$message({
            type: "success",
            message: this.subRemarkId ? "编辑成功" : "添加成功",
          });
          this.subRemarkId = null;
        } else {
          this.$message({
            type: "error",
            message: res.desc,
          });
        }
      });
    },
    // 删除备注
    delSub(item) {
      this.$http
        .post(`/user/delRemark`, {
          remarkId: item.remarkId,
        })
        .then((res) => {
          if (res.code === "0") {
            this.$message({
              type: "success",
              message: "删除成功",
            });
            this.getUserView();
          } else {
            this.$message({
              type: "error",
              message: res.desc,
            });
          }
        });
    },

    // 获取用户详情
    async getUserView() {
      if (this.loading) return;
      this.loading = true;
      let res = await this.$http.get("/user/getUserView", {
        params: {
          userId: this.userId,
        },
      });
      this.loading = false;
      if (res.code === "0") {
        this.properties = res.data.properties;
        this.userVo = res.data.userVo;
        this.userRemarks = res.data.userRemarks;
        this.maxCount = res.data.maxCount;
        this.counts = res.data.counts;
        this.roleArr =
          (res.data.userVo && res.data.userVo.roleNameDescList) || [];
      } else {
        this.$message({
          type: "error",
          message: res.desc,
        });
      }
    },

    // 设为视频合作方
    async updateIdentifys(status) {
      if (this.loading) return;
      this.loading = true;
      let res = await this.$http.get("/user/updateIdentifys", {
        params: {
          userId: this.detail.userId,
          status,
        },
      });
      if (res.code === "0") {
        this.detail.identify = status;
        this.$message({
          type: "success",
          message: "设置成功",
        });
      } else {
        this.$message({
          type: "error",
          message: res.desc,
        });
      }
      this.loading = false;
    },

    // 封禁（解封）用户
    updateStatus(status) {
      this.loading = false
      // 设置正常用户不需要弹框
      if (status === "1") {
        this.status = status;
        this.placeholderTxt = "";
        this.getUpdateStatus();
        return;
      }
      this.banned = true;
      this.status = status;
      let obj = {
        0: "请输入不可提现原因",
        1: "请输入设为正常用户原因",
        "-1": "请输入封禁原因",
        "-2": "请输入冻结钱包原因",
      };
      this.placeholderTxt = obj[status];
    },

    async getUpdateStatus() {
      if (this.loading) return;
      this.loading = true;
      let res = await this.$http.get("/user/updateStatus", {
        params: {
          userId: this.userVo.userId,
          status: this.status,
          message: this.remark,
        },
      });
      this.loading = false
      if (res.code === "0") {
        // 用户状态 -1封禁  0不可提现   1正常用户 -2资金冻结
        this.userVo.userStatus = this.status;
        this.banned = false;
        this.getUserView()
        this.$message({
          type: "success",
          message: "设置成功",
        });
      } else {
        this.$message({
          type: "error",
          message: res.desc,
        });
      }
      this.remark = "";
    },

    // 数字转换
    conversion(val) {
      return conversion(val);
    },

    // 微信评级说明
    setContent(detail) {
      let content = "";
      if (detail.riskType.indexOf("[") > -1) {
        const riskType = JSON.parse(detail.riskType);
        riskType.forEach((item) => {
          content += `<p style="padding: 3px 0;">${item}: ${this.risk[item]};</p>`;
        });
      }
      return content;
    },
  },

  filters: {
    timeInit(val) {
      if (val) {
        return formatDate(val, "YYYY-MM-DD HH:MM:SS");
      } else {
        return "-";
      }
    },
  },

  beforeRouteLeave(to, from, next) {
    delete from.meta.parent;
    console.log(from);
    next();
  },
};
</script>

<style lang="scss" scoped>
.role-wrap {
  padding: 10px;
  display: flex;
}
.user-box {
  margin: 0px auto 0;
  padding-bottom: 40px;
  border-radius: 4px;
  border: solid 1px #dfdfdf;
  border-top: none;
  background-color: #ffffff;

  .sub-wrap {
    border: solid 1px #ededed;
    border-radius: 5px;
    margin: 0px 30px;
    margin-bottom: 20px;
    overflow: hidden;
    .sub-title {
      padding: 10px;
      font-size: 16px;
      font-weight: 500;
      color: #363636;
      position: relative;
    }
    .sub-botton {
      padding: 5px 0px 10px;
      text-align: center;
      border-top: solid 1px #ededed;
      position: relative;
      background-color: #ffffff;
    }
    .sub-list {
      border-top: solid 1px #ededed;
      padding: 10px;
      height: 200px;
      .sub-row {
        margin-bottom: 10px;
        .top {
          display: flex;
          justify-content: flex-start;
          align-items: center;
          margin-bottom: 5px;
          .name {
            font-size: 14px;
            font-weight: 500;
            color: #363636;
            margin-right: 10px;
          }
          .time {
            font-size: 12px;
            font-weight: 500;
            color: #9ba0b1;
            margin-right: 10px;
          }
          i {
            cursor: pointer;
          }
        }
        .bottom {
          font-size: 14px;
          color: #363636;
          word-wrap: break-word;
        }
      }
    }
  }
  .user-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 0 30px;
    margin-bottom: 20px;
    .user-area {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      .user_img {
        width: 50px;
        height: 50px;
        overflow: hidden;
        border-radius: 50px;
        background-color: #ccc;
        margin-right: 20px;
        img {
          width: 50px;
          height: 50px;
        }
      }
      .user-name {
        margin-right: 20px;
        > div {
          font-size: 12px;
          font-weight: 600;
          color: #9ba0b1;
          margin-bottom: 1px;
        }
      }
    }
    .user_btn {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      > div {
        margin-right: 20px;
      }
      // div {
      //     width: 92px;
      //     height: 32px;
      //     border-radius: 4px;
      //     background-color: #0081ff;
      //     display: flex;
      //     justify-content: center;
      //     align-items: center;
      //     font-size: 12px;
      //     font-weight: 500;
      //     color: #ffffff;
      //     cursor: pointer;
      // }
      // .user_btn_center {
      //     margin: 10px 0;
      // }
    }
  }

  .user-msg {
    display: flex;
    align-items: center;
    padding: 20px;
    border-radius: 4px;
    border: solid 1px #ededed;
    background-color: #fdfdfd;
    margin: 0 30px 30px;

    .user-info-box {
      flex: 1;
      margin: 0 20px;
      .user-info-item {
        display: flex;
        margin-bottom: 10px;
        .user_box {
          // padding: 5px;
          // min-width: 100px;
          flex: 1;
        }
        &:last-child {
          margin: 0;
        }
        .rating {
          display: flex;
          align-items: center;
          i {
            cursor: pointer;
            color: #c6c6c6;
            margin-left: 5px;
          }
        }
        .user_msg {
          font-size: 14px;
          font-weight: 500;
          color: #393939;
          display: flex;
          justify-content: flex-start;
          .tip_waring {
            cursor: pointer;
          }
        }
        .user_status {
          font-size: 14px;
          font-weight: 600;
          color: #00ba45;
        }
        .user_status_bad {
          font-size: 14px;
          font-weight: 600;
          color: red;
        }
        .user_title {
          font-size: 12px;
          font-weight: 600;
          color: #9ba0b1;
          margin-bottom: 6px;
        }
      }
    }
  }
  .table-btn {
    cursor: pointer;
    color: #409eff;
    font-size: 13px;
    font-weight: 500;
    &:hover {
      text-decoration: underline;
    }
  }
  .user-tabs {
    margin: 0px 30px;
  }
}
</style>
